﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

/* Aca se definen los eventos que se activan al accionar un control en cada sub-vista.
 * Para cada Sub-Vista hay que definir en cada .ascx.cs los eventos que se usan en los
 * llamadores (onclick p.e.) de cada .ascx.
*/

namespace VirtualTournaments.Frontend.MenuControls.GamblingManagerMenu.InnerControls
{
    using Tournament = Services.Tournaments.Tournament;
    using TournamentsService = Services.Tournaments.TournamentsService;
    using Team = Services.Tournaments.Team;
    using TeamsService = Services.Tournaments.TeamsService;
    using Match = Services.Tournaments.Match;
    using Journey = Services.Tournaments.Journey;

    public partial class GamblingMainMenu : System.Web.UI.UserControl
    {
        private const int AvailableTournamentsViewIndex = 0;
        private const int TeamsSelectionViewIndex = 1;
        private const int JourneySelectionViewIndex = 2;
        private const int TeamInfoViewIndex = 3;

        private int RemovedCharsFromTournamentBetButton = 19;
        private int RemovedCharsFromMatchBetButton = 14;
        
        public EventHandler GamblingMainMenuGoBackButtonClicked;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            this.TournamentBetControl.GoToTeamInfoButton += new EventHandler(TournamentBet_GoToTeamInfoButtonClicked);
            this.TournamentBetControl.GoToCancelBetButton += new EventHandler(TournamentBet_GoToCancelBetButtonClicked);
            this.MatchBetControl.GoToTeamInfoButton += new EventHandler(TournamentBet_GoToTeamInfoButtonClicked);
            this.MatchBetControl.GoToCancelBetButton += new EventHandler(TournamentBet_GoToCancelBetButtonClicked);

            //Tournament[] activeTournaments = TournamentsService.GetActiveTournaments();
            Tournament[] activeTournaments = GetActiveTournaments();

            TableRow newRow;
            TableCell cellT, cellTBet, cellMBet;
            Button tBet, mBet;
            int i = 1;
            foreach (Tournament tournament in activeTournaments)
            {
                newRow = new TableRow();
                
                cellT = new TableCell { Text = tournament.Name, HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center };
                newRow.Cells.Add(cellT);
                
                tBet = new Button { ID = "TournamentBetButton" + i, Text = "Tournament Bet" }; 
                tBet.BorderStyle = BorderStyle.None;
                tBet.BackColor = System.Drawing.Color.Transparent;
                tBet.Click += new EventHandler(TournamentBetButton_OnClick);
                tBet.Attributes.Add("onmouseover", "this.style.cursor='hand'");
                cellTBet = new TableCell { HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center };
                cellTBet.Controls.Add(tBet);
                newRow.Cells.Add(cellTBet);

                mBet = new Button { ID = "MatchBetButton" + i, Text = "Match Bet" };
                mBet.BorderStyle = BorderStyle.None;
                mBet.BackColor = System.Drawing.Color.Transparent;
                mBet.Click += new EventHandler(MatchBetButton_OnClick);
                mBet.Attributes.Add("onmouseover", "this.style.cursor='hand'");
                cellMBet = new TableCell { HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center };
                cellMBet.Controls.Add(mBet);
                newRow.Cells.Add(cellMBet);

                AvailableTournamentsTable.Rows.Add(newRow);
                i++;
            }
        }

        protected void TournamentBetButton_OnClick(object sender, EventArgs e)
        {
            //TODO: what if null?
            Button clickedButton = (Button)sender;
            int tournamentIndex = Convert.ToInt32(clickedButton.ID.Substring(RemovedCharsFromTournamentBetButton, clickedButton.ID.Length - RemovedCharsFromTournamentBetButton));

            this.TournamentBetControl.TournamentName = this.AvailableTournamentsTable.Rows[tournamentIndex].Cells[0].Text;
            Session.Add("CurrentTournament", this.TournamentBetControl.TournamentName);

            //TODO: CONSTANTS!!!
            this.TournamentBetViews.ActiveViewIndex = TeamsSelectionViewIndex;
        }

        protected void MatchBetButton_OnClick(object sender, EventArgs e) 
        {
            //TODO: what if null?
            Button clickedButton = (Button)sender;
            int tournamentIndex = Convert.ToInt32(clickedButton.ID.Substring(RemovedCharsFromMatchBetButton, clickedButton.ID.Length - RemovedCharsFromMatchBetButton));

            this.MatchBetControl.TournamentName = this.AvailableTournamentsTable.Rows[tournamentIndex].Cells[0].Text;

            //TODO: services call
            //this.MatchBetControl.JourneySelectionRadios.Clear();
            //for (int i = 0; i < 2; i++)
            //    this.MatchBetControl.JourneySelectionRadios.Add(new ListItem { Selected = false });

            if (Session["HardcodedJourneys"] == null)
                Session.Add("HardcodedJourneys", SetJourneys());
            
            //TODO: CONSTANTS!!!
            this.TournamentBetViews.ActiveViewIndex = JourneySelectionViewIndex;
        }

        private void TournamentBet_GoToTeamInfoButtonClicked(object sender, EventArgs e)
        {
            this.TournamentBetViews.ActiveViewIndex = TeamInfoViewIndex;
        }

        private void TournamentBet_GoToCancelBetButtonClicked(object sender, EventArgs e)
        {
            this.TournamentBetViews.ActiveViewIndex = AvailableTournamentsViewIndex;
        }

        protected void GamblingMainMenuGoBackButton_OnClick(object sender, EventArgs e) 
        {
            GamblingMainMenuGoBackButtonClicked(this, new EventArgs());
        }

        protected Tournament[] GetActiveTournaments()
        {
            Tournament tournament1, tournament2;
            List<Tournament> tournaments = new List<Tournament>();

            tournament1 = new Tournament("Torneo Apertura 2009", "AllAgainstAllTournament");
            tournament1.IsActive = true;
            tournament2 = new Tournament("Copa Argentina", "PlayOffTournament");
            tournament2.IsActive = true;

            tournaments.Add(tournament1);
            tournaments.Add(tournament2);

            return tournaments.ToArray();
        }

        private List<Journey> SetJourneys()
        {
            List<Journey> journeys = new List<Journey>();
            
            /* Journey 1 */
            Match journey1Match1 = new Match("Arsenal", "Argentinos");
            Match journey1Match2 = new Match("Atlético Tucumán", "Banfield");

            Journey journey1 = new Journey();
            journey1.addMatch(journey1Match1);
            journey1.addMatch(journey1Match2);
            
            /* Journey 2 */
            Match journey2Match1 = new Match("Arsenal", "Atlético Tucumán");
            Match journey2Match2 = new Match("Argentinos", "Banfield");

            Journey journey2 = new Journey();
            journey2.addMatch(journey2Match1);
            journey2.addMatch(journey2Match2);

            /* Journey 3 */
            Match journey3Match1 = new Match("Banfield", "Arsenal");
            Match journey3Match2 = new Match("Atlético Tucumán", "Argentinos");

            Journey journey3 = new Journey();
            journey3.addMatch(journey3Match1);
            journey3.addMatch(journey3Match2);

            journeys.Add(journey1);
            journeys.Add(journey2);
            journeys.Add(journey3);

            return journeys;
        }
    }
}